package logic

import (
	"context"
	"fmt"
	"goods-mngr/Pkg"
	"goods-mngr/cache"
	"math/rand"
	"strconv"
	"time"

	"goods-mngr/goodsrpc/goodsrpc"
	"goods-mngr/goodsrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendsmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendsmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendsmsLogic {
	return &SendsmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendsmsLogic) Sendsms(in *goodsrpc.SendSmsRequest) (*goodsrpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line

	code := rand.Intn(9000) + 1000
	sms, err := Pkg.SendSms(in.Tel, strconv.Itoa(code))
	if err != nil {
		return nil, err
	}
	if *sms.Body.Code != "OK" {
		return nil, fmt.Errorf(*sms.Body.Message)
	}
	cache.Rdb.Set(l.ctx, "SendSms"+in.Tel, sms, time.Minute*2)
	fmt.Println(sms)
	return &goodsrpc.SendSmsResponse{}, nil
}
